package mylibrary.serialize.example;

import static org.junit.Assert.assertEquals;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class SerializableTest {

	@Test
	public void serializeToDisk() throws Exception {
		
		Person gaoshuai = new Person("gao", "shuai", 30);
		Person gaoshuaidage = new Person("gaoshuai", "dage", 20);
		
		gaoshuai.setSpouse(gaoshuaidage);
		gaoshuaidage.setSpouse(gaoshuai);
		
		FileOutputStream fos = new FileOutputStream("c:\\tempData.ser");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(gaoshuai);
		oos.flush();
		
		
		FileInputStream fis = new FileInputStream("c:\\tempData.ser");
		ObjectInputStream ois = new ObjectInputStream(fis);
		Person gaoshuai_r = (Person) ois.readObject();
		ois.close();
		
		assertEquals(gaoshuai_r.getFirstName(), "gao");
		assertEquals(gaoshuai_r.getSpouse().getFirstName(), "gaoshuai");
		assertEquals(gaoshuai_r.getAge(), 30);
	}
}
